//
//  MyAlertView.m
//  JHGJ1.0
//
//  Created by mac2 on 15/7/13.
//  Copyright (c) 2015年 com.yongchuangxianhe. All rights reserved.
//
#import "AppDelegate.h"
#import "MyAlertView.h"
#define SCREEN_WIDTH  ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@implementation MyAlertView
{
   
    UIView *bgView;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
       self.frame = CGRectMake(10, (SCREEN_HEIGHT-66)/2-152/2, SCREEN_WIDTH-20, 152);
        

        
        self.backgroundColor = [UIColor whiteColor];

        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;
 
        
        _lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 40)];
        
        _lab.font = [UIFont systemFontOfSize:15];
        _lab.alpha = 0.8;
        
        [self addSubview:_lab];
        
        
        for(int i = 0 ;i<2;i++)
        {
            UILabel *bgLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50+51*i, SCREEN_WIDTH-20, 1)];
        
        
            bgLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
            [self addSubview:bgLabel];
           
        }
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _btn.frame = CGRectMake(0, 56, SCREEN_WIDTH-20, 40);
        [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        _btn.tag = 0;
        [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _btn.titleEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
        _btn.imageEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
        
        [self addSubview:_btn];
        
        self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        
        self.btn2.frame = CGRectMake(0, 51+51, SCREEN_WIDTH-20, 50);
        [self.btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btn2.tag = 1;
        
        [self.btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        self.btn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.btn2.titleEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
        self.btn2.imageEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
        
        [self addSubview:self.btn2];

   
    }
    
    return self;
}


- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.center = window.center;
    bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.5;

    [window addSubview:bgView];
    [window addSubview:self];
    
    
    UITapGestureRecognizer *recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleTapBehind:)];
    
    [recognizerTap setNumberOfTapsRequired:1];
    recognizerTap.cancelsTouchesInView = NO;
    [bgView addGestureRecognizer:recognizerTap];


}




- (void)handleTapBehind:(UITapGestureRecognizer *)sender{
    if (sender.state == UIGestureRecognizerStateEnded){
        CGPoint location = [sender locationInView:nil];
        
        if (![self pointInside:[self convertPoint:location fromView:self.window] withEvent:nil]){
            [bgView removeFromSuperview];
            [self removeFromSuperview];
            //[self dismissWithClickedButtonIndex:0 animated:YES];
        }
    }
}


- (void)btnClick:(UIButton *)btn
{
    self.clickedBtnIndexBlock(btn.tag);
    [bgView removeFromSuperview];
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [bgView removeFromSuperview];
    [self removeFromSuperview];

    
   
}

@end
